﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OBUActivation.Tags
{
    class PhoneAccess
    {
        

        //添加报文内FCS 
        public static byte[] ComputeFCS(byte[] msgBody)
        {
            int length = msgBody.Length;
            byte temp;
            ushort Crc = 0;
            for(int i = 0; i < length; i++)
            {
                temp = msgBody[i];
                temp = (byte)(temp ^ (byte)(Crc & 0X00FF));
                temp = (byte)(temp ^ (temp << 4));
                Crc = (ushort)((ushort)(Crc >> 8) ^ (ushort)(temp << 8) ^ (ushort)(temp << 3) ^ (ushort)(temp >> 4));
            }
            byte[] fcs = BitConverter.GetBytes(Crc);
            //Array.Copy(msgBody, 0, Msg, 0, length);
            //Array.Copy(fcs, 0, Msg, length - 1, 2);
            return fcs;
        }

        public static byte[] AddFCS(byte[] msgBody)
        {
            byte[] fcs = ComputeFCS(msgBody);
            byte[] Msg = new byte[msgBody.Length + 2];
            int length = msgBody.Length;
            Array.Copy(msgBody, 0, Msg, 0, length);
            Array.Copy(fcs, 0, Msg, length - 1, 2);
            return Msg;
        }



        /* INT16U ComputeCrc(INT8U* Data, INT16U Length)
         {
             INT8U ch;
             INT16U wCrc;
             do
             { ch = *Data++;
               ch = (byte)(ch ^ (byte)(wCrc & 0x00FF));
                 ch = (byte)(ch ^ (ch << 4));
                 wCrc = (ushort)(wCrc >> 8) ^ ((ushort)ch << 8) ^ ((ushort)ch << 3) ^ ((ushort)ch >> 4);
             }
             while (--Length);
             return wCrc; }*/

        public static byte[] AddHeadEnd(byte[] msg)
        {
            List<byte> list = new List<byte>();
            byte head = 0X7E;
            byte end = 0X7E;
            list.Add(head);
            list.AddRange(msg);
            list.Add(end);

            return list.ToArray();
        }

    }


    class BST5 : BaseMsg
    {

        public byte BST;

        public byte[] rsuBeaconid = new byte[4];

        public byte[] unixTime = new byte[4];

        public byte profileInteger;

        public byte mandApplications;

        public byte applicationEntityid;

        public byte profileList;

        public void getBST5(byte[] data)
        {
            byte[] raw = CommonFunc.UnpackageMsg(data);
            if (raw == null)
            {
                return;
            }
            int k = 1;
            flameSTX = raw[k++];
            Array.Copy(raw, k, macAbbr, 0, 4);
            k += 4;
            macControlDomain = raw[k++];
            llcControlDomain = raw[k++];
            segmentInitial = raw[k++];
            BST = raw[k++];
            Array.Copy(raw, k, rsuBeaconid, 0, 4);
            k += 4;
            Array.Copy(raw, k, unixTime, 0, 4);
            k += 4;
            profileInteger = raw[k++];
            mandApplications = raw[k++];
            applicationEntityid = raw[k++];
            profileList = raw[k++];
            Array.Copy(raw, k, FCS, 0, 2);
            k += 2;
            flameETX = raw[k];

        }

        public bool BstIsBST5()
        {
            if (BST == 208)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }


    class VST5 : BaseMsg
    {
        public VST5(byte[] conNum,byte[] rnd)
        {
            //MAC地址是写死成0的
            for(int i = 0; i< macAbbr.Length; i++)
            {
                macAbbr[i] = 0;
            }

            macControlDomain = 192;

            llcControlDomain = 3;

            comtractSerialNumber = conNum;
            byte[] randomNum = rnd;

            rnd1 = randomNum[0];
            rnd2 = randomNum[1];

            profileInteger = 0;

            //貌似是OBU厂商？
            obuMacId[0] = 1;
            obuMacId[1] = 0;
            obuMacId[2] = 0;
            obuMacId[3] = 0;

            //由ESAM给，暂定0
            TamperedInfo = 0;
        }
        public byte profileInteger;

        public byte VST = 209;

        
        public byte Aid = 209;

        public byte did = 3;

        public byte[] comtractSerialNumber;

        public byte rnd1;
        public byte rnd2;

        public byte[] obuMacId ;

        public byte TamperedInfo ;

        //默认OBU和ESAM的OBUMAC ID一致
        public byte obuVersion = 0;

        //电池电量写死成100%电量
        public byte battery = 127;
        

        public byte[] toByteArray()
        {
            List<byte> list = new List<byte>();
            
            list.AddRange(macAbbr);
            list.Add(macControlDomain);
            list.Add(llcControlDomain);
            list.Add(segmentInitial);
            list.Add(VST);
            list.Add(profileInteger);
            list.Add(Aid);
            list.Add(did);
            list.AddRange(comtractSerialNumber);
            list.Add(rnd1);
            list.Add(rnd2);
            list.AddRange(obuMacId);
            list.Add(TamperedInfo);
            list.Add(obuVersion);
            list.Add(battery);
            byte[] msg = list.ToArray();
            PhoneAccess.AddFCS(msg);
            PhoneAccess.AddHeadEnd(msg);
            return msg;
        }
    }
    
}
